import 'package:aichat_flutter_wallet/module/module_browser/viewmodel/before_browser.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/sp.dart';
import 'package:lib_util/common/extentions.dart';

class BrowserInsideDialog extends StatefulWidget {
  bool canCallBack;
  final String url;

  BrowserInsideDialog({super.key, required this.url, this.canCallBack=false});

  @override
  State<BrowserInsideDialog> createState() => _BrowserInsideDialogState();
}

class _BrowserInsideDialogState extends State<BrowserInsideDialog> {
  bool isCheck = false;

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: IntrinsicHeight(
        child: Container(
          padding:
              EdgeInsets.only(top: 15.w, left: 20.w, right: 20.w, bottom: 10.w + MediaQuery.of(context).padding.bottom),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(topLeft: Radius.circular(20.w), topRight: Radius.circular(20.w)),
          ),
          child: Column(
            children: [
              Align(
                alignment: Alignment.centerLeft,
                child: InkWell(
                  child: Image.asset(
                    'assets/images/dapp/close.png',
                    width: 20.w,
                    height: 20.w,
                  ),
                  onTap: () {
                    Navigator.of(context).pop(!widget.canCallBack?null:'Failed');
                  },
                ),
              ),
              SizedBox(
                height: 15.w,
              ),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Image.asset(
                    'assets/images/dapp/warn.png',
                    width: 20.w,
                    height: 20.w,
                  ),
                  SizedBox(
                    width: 3.w,
                  ),
                  Text(
                    'Warm Reminder',
                    style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: '#333333'.toColor()),
                  )
                ],
              ),
              SizedBox(
                height: 8.w,
              ),
              Text(
                StringExt.toCharacterBreakStr('You are about to enter the third-party DApp《${widget.url}》'),
                softWrap: true,
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: FontWeight.w400,
                  color: '#868391'.toColor(),
                ),
              ),
              SizedBox(
                height: 4,
              ),
              Text(
                '''your usage of the third-party DApp is subject to its 'Privacy Policy' and 'User Agreement', for which 'Eth2.0 Stakingbears direct and separate responsibility to you.''',
                softWrap: true,
                style: TextStyle(
                    fontSize: 12.sp, fontWeight: FontWeight.w400, height: (16 / 12).w, color: '#BEBDCA'.toColor()),
              ),
              SizedBox(
                height: 15.w,
              ),
              Container(
                padding: EdgeInsets.only(top: 10.w, left: 15.w, right: 15.w, bottom: 15.w),
                decoration: BoxDecoration(color: '#F6F6F6'.toColor(), borderRadius: BorderRadius.circular(10.w)),
                child: Column(
                  children: [
                    Text(
                      // 'Please make sure you understand the following',
                      StringExt.toCharacterBreakStr(
                        'Please make sure you understand the following',
                      ),
                      softWrap: true,
                      style: TextStyle(
                          fontSize: 14.sp,
                          color: '#333333'.toColor(),
                          fontWeight: FontWeight.w500,
                          height: 20.w / 14.w),
                    ),
                    SizedBox(
                      height: 10.w,
                    ),
                    Text.rich(
                      TextSpan(children: [
                        TextSpan(
                            text: '• ',
                            style: TextStyle(
                              fontSize: 12.sp,
                              height: 16.w / 12.w,
                              color: '#868391'.toColor(),
                            )),
                        TextSpan(
                          text: StringExt.toCharacterBreakStr(
                              'This DApp is not owned by KAI and is fully controlled by the project team! Please ensure you are aware of the security of this DApp beore using it.'),
                          style: TextStyle(
                            height: 16.w / 12.w,
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400,
                            color: '#868391'.toColor(),
                          ),
                        ),
                      ]),
                    ),
                    SizedBox(
                      height: 10.w,
                    ),
                    Text.rich(
                      TextSpan(children: [
                        TextSpan(
                            text: '• ',
                            style: TextStyle(
                              fontSize: 12.sp,
                              height: 16.w / 12.w,
                              color: '#868391'.toColor(),
                            )),
                        TextSpan(
                          text: StringExt.toCharacterBreakStr(
                              'You are aware that any losses incurred as a result of your access to this Dapp are your personal responsibility and not related to Kai.'),
                          style: TextStyle(
                            fontSize: 12.sp,
                            height: 16.w / 12.w,
                            fontWeight: FontWeight.w400,
                            color: '#868391'.toColor(),
                          ),
                        ),
                      ]),
                    ),
                    SizedBox(
                      height: 10.w,
                    ),
                    Text.rich(
                      TextSpan(children: [
                        TextSpan(
                            text: '• ',
                            style: TextStyle(
                              fontSize: 12.sp,
                              height: 16.w / 12.w,
                              color: '#868391'.toColor(),
                            )),
                        TextSpan(
                          text: StringExt.toCharacterBreakStr(
                              'If you encounter any issues while usingis DApp, you need to contact the project team directly foresolution.'),
                          style: TextStyle(
                            fontSize: 12.sp,
                            height: 16.w / 12.w,
                            fontWeight: FontWeight.w400,
                            color: '#868391'.toColor(),
                          ),
                        ),
                      ]),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 15.w,
              ),
              InkWell(
                splashColor: Colors.transparent,
                highlightColor: Colors.transparent,
                onTap: () {
                  setState(() {
                    isCheck = !isCheck;
                  });
                },
                child: Row(
                  children: [
                    isCheck
                        ? Image.asset(
                            'assets/images/dapp/checked.png',
                            width: 20.w,
                            height: 20.w,
                          )
                        : Container(
                            width: 20.w,
                            height: 20.w,
                            decoration: BoxDecoration(
                              border: Border.all(color: '#BEBDCA'.toColor(), width: 1.w),
                              borderRadius: BorderRadius.circular(4.w),
                            ),
                          ),
                    SizedBox(
                      width: 5.w,
                    ),
                    Text(
                      "Don't promot again",
                      style: TextStyle(color: '#868391'.toColor(), fontSize: 12),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 19.w,
              ),
              InkWell(
                onTap: () {
                  if (isCheck) {
                    List<String> list = SP().getBrowserInsideValid();
                    list.add(widget.url);
                    SP().setBrowserInsideValid(list);
                  }
                  if(widget.canCallBack){
                    Navigator.of(context).pop('Success');
                    return;
                  }
                  Navigator.of(context).pop();
                  goBrowser(url: widget.url);
                },
                child: Container(
                  height: 46.w,
                  width: 335.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(color: '#8667FE'.toColor(), borderRadius: BorderRadius.circular(10.w)),
                  child: Text(
                    'I see',
                    style: TextStyle(
                      color: '#ffffff'.toColor(),
                      fontSize: 12.sp,
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
